package com.tedu.cloud_vehicle_plus.controller;


import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.tedu.cloud_vehicle_plus.base.response.JsonResult;
import com.tedu.cloud_vehicle_plus.base.utils.CacheUtil;
import com.tedu.cloud_vehicle_plus.mapper.NoticeMapper;
import com.tedu.cloud_vehicle_plus.pojo.vo.NoticeVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.Serializable;
import java.util.List;

@Api(tags = "03.公告管理模块")
@Slf4j
@RestController
@RequestMapping("/v1/notice/")
public class NoticeController {
    @Autowired
    private NoticeMapper noticeMapper;
    @Autowired
    private CacheUtil cacheUtil;

    @GetMapping("list")
    @ApiOperation(value = "首页公告展示")
    @ApiOperationSupport(order = 10)
    public JsonResult list() {
        Serializable noticeIndex = cacheUtil.get("notice_index");
        if (noticeIndex == null) {
            log.debug("首页数据来源于MySQL");
            List<NoticeVO> noticeVOList = noticeMapper.selectNoticeIndex();
            cacheUtil.setExpire("notice_index", (Serializable) noticeVOList, 30);
            return JsonResult.ok(noticeVOList);
        }
        log.debug("首页数据来源于Redis");
        return JsonResult.ok(noticeIndex);
    }
}
